package cn.lhl01.sys.controller;

import cn.hutool.core.util.StrUtil;
import cn.lhl01.common.Results;
import cn.lhl01.sys.constant.IdsConstant;
import cn.lhl01.sys.constant.PubUriConstant;
import cn.lhl01.sys.entity.vo.LoginVo;
import cn.lhl01.sys.entity.vo.TestVo;
import cn.lhl01.sys.service.impl.SysUserServiceImp;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * Controller统一使用post请求
 */
@Slf4j
@RestController
@RequiredArgsConstructor
public class LoginController {

	@NonNull
	private SysUserServiceImp sysUserService;

/*	@NonNull
	private RestTemplate restTemplate;*/


	/**
	 * 基于form 或url 通过用户标识 密码 登录
	 *
	 * @param username
	 * @param password
	 * @return
	 * @RequestParam 注解把url中的参数 映射到方法参数上
	 */
	@PostMapping("/login2")
	public Results login2(@RequestParam(required = false) String username, @RequestParam(required = false) String password) {
		if (StrUtil.isNullOrUndefined(username)) {
			return Results.paramError("用编号不能为空");
		}

		if (StrUtil.isNullOrUndefined(password)) {
			return Results.paramError("密码不能为空");
		}

		return sysUserService.login(username, password);
	}

	/**
	 * 基于json 通过用户标识 密码 登录
	 *
	 * @param loginReq
	 * @return
	 */
	@PostMapping("/login")
	public Results login(@RequestBody @Valid LoginVo loginReq) {

		return sysUserService.login(loginReq.getUsername(), loginReq.getPassword());
	}

	/**
	 * 基于统一身份的登录 form表单
	 *
	 * @param token
	 * @return
	 */
	@PostMapping(PubUriConstant.PERMITALL.LOGINSSO)
	public String ssoLogin(@RequestParam(required = false) String token) throws Exception {
		if (StringUtils.isBlank(token)) {
			throw new Exception("token不能为空");
		}

		//设置请求头, x-www-form-urlencoded格式的数据
		HttpHeaders httpHeaders = new HttpHeaders();
		httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
		httpHeaders.add("token", token);
		//appkey
		httpHeaders.add("appkey", IdsConstant.IDSAPPKEY);
		//组装请求体
		HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(httpHeaders);
		//统一身份认证 验证token
		//Object s1 = restTemplate.postForObject(IdsConstant.IDSURL + IdsConstant.GET_USERINFO_PATH, request, Object.class);

//		System.out.println(s1);
//		if (StringUtils.isBlank(s1.toString())) {
//			throw new Exception("获取sso用户信息失败");
//		}


		return null;
	}


	/**
	 * test 一个耗时的操作
	 *
	 * @param testVo
	 * @return
	 * @throws InterruptedException
	 */
	@PostMapping(PubUriConstant.AUTHENTICATED.gogogo222)
	//@PreAuthorize("hasAuthority('/gogogo222')") //springboot默认参数校验在 权限校验之前  转发的也需要权限
	public Results gogogo222(@Valid TestVo testVo) throws InterruptedException {

		log.info("sssgogogo222");
		log.info(testVo.toString());
		Thread.sleep(1000);
		log.info("eeegogogo222");
		return Results.success("ok");
	}

}